

package org.lq.ht.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.lq.ht.dao.DictionaryDao;
import org.lq.ht.entity.Dictionary;
import org.lq.ht.util.Instantiation;
import org.lq.ht.util.JDBCUtil;


/**
 * 字典接口实现类
 * @author 郑奥宇
 * @phone 15993025053
 *
 * @package org.lq.ht.dao.impl
 * @date 2020年8月20日下午10:02:43
 */
public class DictionaryDaoImpl implements DictionaryDao,Instantiation<Dictionary>{
	/**
	 * 添加
	 */
	@Override
	public int save(Dictionary t) {
		int num = 0;
		String sql = "insert into dictionary(name,type_id,note) values(?,?,?)";
		try {
			num = JDBCUtil.executeUpdate(sql, t.getName(),t.getType_id(),t.getNote());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return num;
	}
	/**
	 * 修改
	 */
	@Override
	public int update(Dictionary t) {
		int num = 0;
		String sql = "update dictionary set name = ?,type_id=?,note=? where no = ?";
		try {
			num = JDBCUtil.executeUpdate(sql, t.getName(),t.getType_id(),t.getNote(),t.getNo());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return num;
	}
	/**
	 * 删除
	 */
	@Override
	public int delete(int no) {
		int num = 0;
		String sql = "delete from dictionary where no = ?";
		try {
			num = JDBCUtil.executeUpdate(sql, no);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return num;

	}
	/**
	 * 查询全部
	 */
	@Override
	public List<Dictionary> findAll() {
		List<Dictionary> list = new ArrayList<>();
		String sql = "select * from dictionary";
		try {
			list = JDBCUtil.executeQuery(sql, this);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * 通过序号查询
	 */
	@Override
	public Dictionary getById(int no) {
		List<Dictionary> list = new ArrayList<>();
		String sql = "select * from dictionary where no = ?";
		try {
			list = JDBCUtil.executeQuery(sql, this, no);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (list.size()>0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public Dictionary instance(ResultSet rs) {
		Dictionary dic = new Dictionary();
		try {
			dic.setName(rs.getString("name"));
			dic.setType_id(rs.getInt("type_id"));
			dic.setNote(rs.getString("note"));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return dic;
	}
	@Override
	public List<Dictionary> findDictionaryByTypeID(int tid) {
		try {
			return JDBCUtil.executeQuery("select * from dictionary where type_id = ?", this, tid);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}

